<!DOCTYPE HTML>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
description("Test WebSocket permessage-deflate extension: window bits.");

window.jsTestIsAsync = true;

var closeEvent;
var ws;
var messageIndex;

var queries = [
    "client_max_window_bits=8",
    "client_max_window_bits=15",
    "client_no_context_takeover",
    "client_max_window_bits=8&client_no_context_takeover"
];

// We construct this message so that the test will fail if the client deflater
// doesn't care about client_max_window_bits.
var alpha = 'abcdefghijklmnopqrstuvwxyz'
var message = '';
for (var i = 0; i < 30000; ++i) {
    message += '-';
}
message = alpha + message + alpha;

function doTest(queryIndex)
{
    var query = queries[queryIndex];
    debug("Testing query: \"" + query + "\"");

    var url = "ws://localhost:8880/permessage-deflate-manual?" + query;
    ws = new WebSocket(url);

    ws.onopen = function(event)
    {
        shouldBeTrue("ws.extensions.search('permessage-deflate') != -1");
        parameters = query.split('&');
        for (var i = 0; i < parameters.length; ++i)
            shouldBeTrue("ws.extensions.search('" + parameters[i] + "') != -1");
        ws.send(message);
    };

    ws.onmessage = function(event)
    {
        // Because message is too long and shouldBe tends to be timed out on fail,
        // we use custom output rather than shouldBe.
        if (event.data === message) {
            debug('PASS event.data is message');
        } else {
            debug('FAIL event.data should be message but is ' + event.data);
        }
        ws.close();
    };

    ws.onclose = function(event)
    {
        debug("onclose() was called.");
        closeEvent = event;
        shouldBeTrue("closeEvent.wasClean");
        if (queryIndex === queries.length - 1)
            finishJSTest();
        else
            doTest(queryIndex + 1);
    };
}

doTest(0);

</script>
</body>
</html>
